package com.wizinno.android.urineflow.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.wizinno.android.urineflow.R;
import com.wizinno.android.urineflow.constant.FragmentNoConstant;
import com.wizinno.android.urineflow.data.SteeringStatusEnum;
import com.wizinno.android.urineflow.data.ValveStatusEnum;
import com.wizinno.android.urineflow.serialport.SerialPortUtil;
import com.wizinno.android.urineflow.serialport.SerialProtocol;

public class SteeringFragment extends BaseFragment {

    private TextView tv_valve_status;
    private Button btn_valve_open;
    private Button btn_valve_close;

    private Button btn_steering_0;
    private Button btn_steering_90;
    private Button btn_steering_wash;
    private Button btn_steering_read;

    private TextView tv_steering_status;
    private EditText et_current_degree;
    private EditText et_current_encoder;

    public SteeringFragment() {
        setNo(FragmentNoConstant.SETTING_STEERING);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tv_valve_status = view.findViewById(R.id.tv_valve_status);
        btn_valve_open = view.findViewById(R.id.btn_valve_open);
        btn_valve_close = view.findViewById(R.id.btn_valve_close);
        btn_valve_open.setOnClickListener(new ValveClickListener());
        btn_valve_close.setOnClickListener(new ValveClickListener());

        tv_steering_status = view.findViewById(R.id.tv_steering_status);
        et_current_degree = view.findViewById(R.id.et_current_degree);
        et_current_encoder = view.findViewById(R.id.et_current_encoder);

        btn_steering_0 = view.findViewById(R.id.btn_steering_0);
        btn_steering_90 = view.findViewById(R.id.btn_steering_90);
        btn_steering_wash = view.findViewById(R.id.btn_steering_wash);
        btn_steering_read = view.findViewById(R.id.btn_steering_read);

        SteeringClickListener steeringClickListener = new SteeringClickListener();
        btn_steering_0.setOnClickListener(steeringClickListener);
        btn_steering_90.setOnClickListener(steeringClickListener);
        btn_steering_wash.setOnClickListener(steeringClickListener);
        btn_steering_read.setOnClickListener(steeringClickListener);
    }

    @Override
    protected void initData() {
        SerialPortUtil.readValveStatus();
        try {
            Thread.sleep(200);
            SerialPortUtil.readSteeringStatus();
            Thread.sleep(200);
            SerialPortUtil.readSteeringAngle();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_steering, container, false);
    }

    public void setSteeringStatus(byte[] receiveBytes) {
        if (receiveBytes.length >= 3) {
            byte status = receiveBytes[2];
            String prefix = "容器姿态：";
            String text = prefix + SteeringStatusEnum.getDesc(status);
            tv_steering_status.setText(text);
        }
    }

    public void setSteeringCurrent(byte[] receiveBytes) {
        if (receiveBytes.length >= 6) {
            Integer degree = SerialPortUtil.byte2TransToInt(receiveBytes[2], receiveBytes[3]);
            Integer encoder = SerialPortUtil.byte2TransToInt(receiveBytes[4], receiveBytes[5]);
            et_current_degree.setText(degree);
            et_current_encoder.setText(encoder);
        }
    }

    public void setValveStatus(byte[] receiveBytes) {
        if (receiveBytes.length >= 3) {
            //取低位
            int status = receiveBytes[2] & 0x0F;
            String prefix = "阀门状态：";
            String desc = prefix + status + "(" + ValveStatusEnum.getDesc(status) + ")";
            tv_valve_status.setText(desc);
        }
    }

    class ValveClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
            if (viewId == R.id.btn_valve_close) {
                SerialPortUtil.writeValveStatus(SerialProtocol.VALVE_STATUS_CLOSE);
            } else if (viewId == R.id.btn_valve_open) {
                SerialPortUtil.writeValveStatus(SerialProtocol.VALVE_STATUS_OPEN);
            }
        }
    }

    class SteeringClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
            switch (viewId) {
                case R.id.btn_steering_0:
                    SerialPortUtil.steering0();
                    break;
                case R.id.btn_steering_90:
                    SerialPortUtil.steering90();
                    break;
                case R.id.btn_steering_wash:
                    SerialPortUtil.steeringWash();
                    break;
                case R.id.btn_steering_read:
                    SerialPortUtil.readSteeringAngle();
                    break;
            }
        }
    }
}